########################################
# Adult sacred-rite participants
########################################

cr_orthodox_find_rite_participants_effect = {
	clear_event_target = cr_orthodox_rite_partner_1
	clear_event_target = cr_orthodox_rite_partner_2
	location = {
		random_province_character = {
			limit = { cr_valid_intimacy_partner_trigger = yes }
			save_event_target_as = cr_orthodox_rite_partner_1
		}
		random_province_character = {
			limit = {
				cr_valid_intimacy_partner_trigger = yes
				NOT = { character = event_target:cr_orthodox_rite_partner_1 }
			}
			save_event_target_as = cr_orthodox_rite_partner_2
		}
	}
}

cr_orthodox_clear_rite_participants_effect = {
	clear_event_target = cr_orthodox_rite_partner_1
	clear_event_target = cr_orthodox_rite_partner_2
	clear_event_target = cr_pilgrimage_companion
	clear_event_target = cr_intimacy_partner
}

cr_orthodox_resolve_group_rite_effect = {
	if = {
		limit = { event_target:cr_orthodox_rite_partner_1 = { cr_valid_intimacy_partner_trigger = yes } }
		event_target:cr_orthodox_rite_partner_1 = { save_event_target_as = cr_intimacy_partner }
		cr_record_contextual_intimacy_effect = yes
	}
	if = {
		limit = { event_target:cr_orthodox_rite_partner_2 = { cr_valid_intimacy_partner_trigger = yes } }
		event_target:cr_orthodox_rite_partner_2 = { save_event_target_as = cr_intimacy_partner }
		cr_record_contextual_intimacy_effect = yes
	}
	random_list = {
		65 = {
			modifier = { factor = 1.50 diplomacy = 15 }
			modifier = { factor = 1.40 intrigue = 15 }
			modifier = { factor = 1.35 trait = lustful }
			modifier = { factor = 1.30 trait = hedonist }
			modifier = { factor = 1.30 trait = exhibitionist }
			modifier = { factor = 1.25 trait = socializer }
			modifier = { factor = 0.65 trait = shy }
			piety = 60
			prestige = 50
			cr_gain_carnal_good_ending_trait_effect = yes
		}
		35 = {
			modifier = { factor = 1.60 NOT = { diplomacy = 8 } }
			modifier = { factor = 1.45 NOT = { intrigue = 8 } }
			modifier = { factor = 1.35 trait = shy }
			modifier = { factor = 0.65 diplomacy = 16 }
			modifier = { factor = 0.70 trait = hedonist }
			piety = 10
			prestige = -25
			cr_gain_intrigue_bad_ending_trait_effect = yes
			random = { chance = 20 if = { limit = { NOT = { trait = stressed } } add_trait = stressed } }
		}
	}
	cr_orthodox_clear_rite_participants_effect = yes
}

########################################
# Five-see pilgrimage completion
########################################

cr_orthodox_finish_jerusalem_effect = {
	if = { limit = { NOT = { trait = cr_orthodox_pilgrim_jerusalem } } add_trait = cr_orthodox_pilgrim_jerusalem }
	cr_pilgrimage_begin_return_effect = yes
	cr_orthodox_clear_rite_participants_effect = yes
}

cr_orthodox_finish_constantinople_effect = {
	if = { limit = { NOT = { trait = cr_orthodox_pilgrim_constantinople } } add_trait = cr_orthodox_pilgrim_constantinople }
	cr_pilgrimage_begin_return_effect = yes
	cr_orthodox_clear_rite_participants_effect = yes
}

cr_orthodox_finish_antioch_effect = {
	if = { limit = { NOT = { trait = cr_orthodox_pilgrim_antioch } } add_trait = cr_orthodox_pilgrim_antioch }
	cr_pilgrimage_begin_return_effect = yes
	cr_orthodox_clear_rite_participants_effect = yes
}

cr_orthodox_finish_rome_effect = {
	if = { limit = { NOT = { trait = cr_orthodox_pilgrim_rome } } add_trait = cr_orthodox_pilgrim_rome }
	cr_pilgrimage_begin_return_effect = yes
	cr_orthodox_clear_rite_participants_effect = yes
}

cr_orthodox_finish_alexandria_effect = {
	if = { limit = { NOT = { trait = cr_orthodox_pilgrim_alexandria } } add_trait = cr_orthodox_pilgrim_alexandria }
	cr_pilgrimage_begin_return_effect = yes
	cr_orthodox_clear_rite_participants_effect = yes
}

########################################
# Coronation
########################################

cr_orthodox_coronation_donation_effect = {
	if = {
		limit = { wealth = 1 }
		export_to_variable = { which = cr_orthodox_coronation_cost value = wealth }
		multiply_variable = { which = cr_orthodox_coronation_cost value = -0.25 }
		wealth = cr_orthodox_coronation_cost
	}
	else = { wealth = -1 }
}

cr_orthodox_complete_coronation_effect = {
	clr_character_flag = cr_orthodox_coronation_pending
	clr_character_flag = cr_orthodox_coronation_donation
	clr_character_flag = cr_orthodox_coronation_penance
	set_character_flag = cr_orthodox_crowned
	remove_character_modifier = cr_orthodox_uncrowned
	add_character_modifier = { name = cr_orthodox_consecrated duration = -1 }
	piety = 300
	prestige = 500
	if = {
		limit = { persistent_event_target:cr_orthodox_coronator = { is_alive = yes } }
		persistent_event_target:cr_orthodox_coronator = {
			opinion = { modifier = cr_orthodox_coronation_gratitude who = ROOT years = 20 }
		}
	}
	clear_persistent_event_target = cr_orthodox_coronator
	set_variable = { which = cr_orthodox_coronation_cost value = 0 }
}

########################################
# Synodal punishment and reconciliation
########################################

cr_orthodox_pilgrimage_disband_synodal_hosts_effect = {
	if = {
		limit = { persistent_event_target:cr_orthodox_pilgrimage_legate = { always = yes } }
		persistent_event_target:cr_orthodox_pilgrimage_legate = {
			disband_event_forces = cr_orthodox_synodal_host_1
			disband_event_forces = cr_orthodox_synodal_host_2
			disband_event_forces = cr_orthodox_synodal_host_3
			disband_event_forces = cr_orthodox_synodal_host_4
		}
	}
}

cr_orthodox_pilgrimage_clear_crisis_state_effect = {
	cr_orthodox_pilgrimage_disband_synodal_hosts_effect = yes
	clr_character_flag = cr_orthodox_pilgrimage_crisis
	clr_character_flag = cr_orthodox_penance_pending
	remove_character_modifier = cr_orthodox_pilgrimage_crisis_modifier
	clear_persistent_event_target = cr_orthodox_pilgrimage_legate
}

cr_orthodox_pilgrimage_end_synodal_wars_effect = {
	any_war = {
		limit = {
			OR = {
				using_cb = cr_synodal_chastisement
				using_cb = cr_orthodox_crusade_of_penance
			}
		}
		end_war = invalid
	}
}

cr_orthodox_pilgrimage_reconcile_effect = {
	cr_orthodox_pilgrimage_end_synodal_wars_effect = yes
	cr_orthodox_pilgrimage_clear_crisis_state_effect = yes
	excommunicate = no
	add_character_modifier = { name = cr_orthodox_pilgrimage_reconciled duration = 1825 }
	if = {
		limit = { rightful_religious_head_scope = { is_alive = yes } }
		rightful_religious_head_scope = {
			opinion = { modifier = cr_orthodox_synodal_reconciliation who = ROOT years = 10 }
		}
	}
}

cr_orthodox_pilgrimage_create_chastisement_effect = {
	save_event_target_as = cr_orthodox_pilgrimage_offender

	if = {
		limit = { persistent_event_target:cr_orthodox_pilgrimage_legate = { always = yes } }
		persistent_event_target:cr_orthodox_pilgrimage_legate = {
			disband_event_forces = cr_orthodox_synodal_host_1
			disband_event_forces = cr_orthodox_synodal_host_2
			disband_event_forces = cr_orthodox_synodal_host_3
			disband_event_forces = cr_orthodox_synodal_host_4
			if = { limit = { is_alive = yes war = no } death = yes }
		}
		clear_persistent_event_target = cr_orthodox_pilgrimage_legate
	}

	496 = {
		create_character = {
			random_traits = no
			dynasty = none
			religion = orthodox
			culture = greek
			female = no
			age = 40
			health = 7
			fertility = 0
			trait = zealous
			trait = brilliant_strategist
			trait = brave
			attributes = {
				diplomacy = 12
				martial = 18
				stewardship = 10
				intrigue = 12
				learning = 15
			}
		}
		new_character = {
			set_character_flag = cr_orthodox_synodal_legate
			save_event_target_as = cr_new_orthodox_synodal_legate
			create_title = {
				tier = DUKE
				landless = yes
				temporary = yes
				short_name = yes
				culture = THIS
				name = CR_ORTHODOX_SYNODAL_HOST
				ruler = CR_ORTHODOX_SYNODAL_LEGATE
				ruler_female = CR_ORTHODOX_SYNODAL_LEGATE_FEMALE
				holder = THIS
			}
		}
	}

	save_persistent_event_target = {
		name = cr_orthodox_pilgrimage_legate
		scope = event_target:cr_new_orthodox_synodal_legate
	}
	event_target:cr_new_orthodox_synodal_legate = { save_event_target_as = cr_orthodox_pilgrimage_war_legate }

	event_target:cr_new_orthodox_synodal_legate = {
		spawn_unit = {
			province = 496 home = 496 owner = THIS leader = THIS earmark = cr_orthodox_synodal_host_1
			cannot_inherit = yes disband_on_peace = yes attrition = 0.05 maintenance_multiplier = 0 reinforces = yes reinforce_rate_multiplier = 1.0
			troops = { light_infantry = { 2000 2000 } heavy_infantry = { 2500 2500 } pikemen = { 1500 1500 } archers = { 2000 2000 } light_cavalry = { 1000 1000 } knights = { 1000 1000 } }
		}
		spawn_unit = {
			province = 496 home = 496 owner = THIS leader = THIS earmark = cr_orthodox_synodal_host_2
			cannot_inherit = yes disband_on_peace = yes attrition = 0.05 maintenance_multiplier = 0 reinforces = yes reinforce_rate_multiplier = 1.0
			troops = { light_infantry = { 2000 2000 } heavy_infantry = { 2500 2500 } pikemen = { 1500 1500 } archers = { 2000 2000 } light_cavalry = { 1000 1000 } knights = { 1000 1000 } }
		}
		spawn_unit = {
			province = 496 home = 496 owner = THIS leader = THIS earmark = cr_orthodox_synodal_host_3
			cannot_inherit = yes disband_on_peace = yes attrition = 0.05 maintenance_multiplier = 0 reinforces = yes reinforce_rate_multiplier = 1.0
			troops = { light_infantry = { 2000 2000 } heavy_infantry = { 2500 2500 } pikemen = { 1500 1500 } archers = { 2000 2000 } light_cavalry = { 1000 1000 } knights = { 1000 1000 } }
		}
		spawn_unit = {
			province = 496 home = 496 owner = THIS leader = THIS earmark = cr_orthodox_synodal_host_4
			cannot_inherit = yes disband_on_peace = yes attrition = 0.05 maintenance_multiplier = 0 reinforces = yes reinforce_rate_multiplier = 1.0
			troops = { light_infantry = { 2000 2000 } heavy_infantry = { 2500 2500 } pikemen = { 1500 1500 } archers = { 2000 2000 } light_cavalry = { 1000 1000 } knights = { 1000 1000 } }
		}
	}

	if = {
		limit = { has_global_flag = christian_crusades_unlocked }
		unsafe_war = { target = event_target:cr_new_orthodox_synodal_legate casus_belli = cr_orthodox_crusade_of_penance }
	}
	else = {
		unsafe_war = { target = event_target:cr_new_orthodox_synodal_legate casus_belli = cr_synodal_chastisement }
	}

	any_character = {
		limit = {
			is_alive = yes
			is_ruler = yes
			is_landed = yes
			higher_tier_than = BARON
			religion = orthodox
			NOT = { character = ROOT }
			NOT = { character = event_target:cr_new_orthodox_synodal_legate }
		}
		if = {
			limit = { ai = yes }
			join_defender_wars = event_target:cr_orthodox_pilgrimage_war_legate
		}
		else = { long_character_event = { id = cr_orthodox_punishment.21 } }
	}
}

cr_orthodox_pilgrimage_start_crisis_effect = {
	cr_pilgrimage_clear_journey_effect = yes
	set_character_flag = cr_orthodox_pilgrimage_crisis
	excommunicate = yes
	add_character_modifier = { name = cr_orthodox_pilgrimage_crisis_modifier duration = -1 }
	cr_orthodox_pilgrimage_create_chastisement_effect = yes
}

cr_orthodox_penance_tithe_effect = {
	if = {
		limit = { wealth = 0 }
		export_to_variable = { which = cr_pilgrimage_monthly_cost value = wealth }
		multiply_variable = { which = cr_pilgrimage_monthly_cost value = -0.50 }
		wealth = cr_pilgrimage_monthly_cost
	}
	piety = -500
	cr_orthodox_pilgrimage_reconcile_effect = yes
}

cr_orthodox_penance_abasement_effect = {
	prestige = -1500
	piety = -750
	if = { limit = { trait = proud } remove_trait = proud }
	if = { limit = { NOT = { trait = humble } } add_trait = humble }
	cr_orthodox_pilgrimage_reconcile_effect = yes
}

cr_orthodox_penance_submission_effect = {
	if = {
		limit = { wealth = 0 }
		export_to_variable = { which = cr_pilgrimage_monthly_cost value = wealth }
		multiply_variable = { which = cr_pilgrimage_monthly_cost value = -0.25 }
		wealth = cr_pilgrimage_monthly_cost
	}
	prestige = -750
	piety = -1000
	cr_orthodox_pilgrimage_reconcile_effect = yes
}
